package com.appspot.niehonglei.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;

import com.appspot.niehonglei.dao.PMF;
import com.appspot.niehonglei.jdo.Comment;
import com.appspot.niehonglei.jdo.Index;

/**
 * 公共使用类
 * 
 * @author niehonglei
 * 
 */
public class Tools {

	/**
	 * 转换东八区时间和标准时间
	 * 
	 * @param date
	 * @return
	 */
	// private final static String regxpForHtml = "<([^>]*)>"; //
	// 过滤所有以<开头以>结尾的标签
	// private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>";
	// // 找出IMG标签
	//
	// private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\"";
	// // 找出IMG标签的SRC属性
	public static Date shiftDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR, 8);
		return c.getTime();

	}

	/**
	 * 将日期按照yyyy-MM-dd HH:mm:ss转换为字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String date2str(Date date) {
		String pattern = "yyyy-MM-dd HH:mm:ss"; // 定义显示的形式
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 处理字符串中的空指针
	 * 
	 * @param str
	 * @return
	 */
	public static String dealNull(String str) {
		return (str == null || "null".equals(str.trim())) ? "" : str.trim();
	}

	/**
	 * 根据分类来统计index中记录的数量
	 * 
	 * @param category
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static int countIndex(String category) {
		int count = 0;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			javax.jdo.Query query = pm.newQuery(Index.class);
			if (category != null && !"".equals(category)) {
				query.setFilter(" category == '" + category + "'");
			}
			List<Index> results = (List<Index>) query.execute();
			count = results.size();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return count;
	}

	/**
	 * 根据文章id号统计评论数
	 * 
	 * @param indexId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static int countComment(String indexId) {
		int count = 0;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String where = "";
			if (!"".equals(Tools.dealNull(indexId))) {
				where = "  where ifPublish == 'true' && indexId == " + indexId;
			}
			String query = "select from " + Comment.class.getName() + where;
			List<Comment> list = (List<Comment>) pm.newQuery(query).execute();
			count = list.size();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return count;
	}

	public static boolean checkEmail(String email) {
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(email);
		return m.find();
	}

	/**
	 * 
	 * 基本功能：替换标记以正常显示
	 * <p>
	 * 
	 * @param input
	 * @return String
	 */
	public static String replaceTag(String input) {
		if (!hasSpecialChars(input)) {
			return input;
		}
		StringBuffer filtered = new StringBuffer(input.length());
		char c;
		for (int i = 0; i <= input.length() - 1; i++) {
			c = input.charAt(i);
			switch (c) {
			case '<':
				filtered.append("&lt;");
				break;
			case '>':
				filtered.append("&gt;");
				break;
			case '"':
				filtered.append("&quot;");
				break;
			case '&':
				filtered.append("&amp;");
				break;
			default:
				filtered.append(c);
			}

		}
		return (filtered.toString());
	}

	/**
	 * 
	 * 基本功能：判断标记是否存在
	 * <p>
	 * 
	 * @param input
	 * @return boolean
	 */
	public static boolean hasSpecialChars(String input) {
		boolean flag = false;
		if ((input != null) && (input.length() > 0)) {
			char c;
			for (int i = 0; i <= input.length() - 1; i++) {
				c = input.charAt(i);
				switch (c) {
				case '>':
					flag = true;
					break;
				case '<':
					flag = true;
					break;
				case '"':
					flag = true;
					break;
				case '&':
					flag = true;
					break;
				}
			}
		}
		return flag;
	}

}
